<?php
class controller_box extends model_box{

    public function mail_add()
    {
        $touid = $this->existsKeyInArray('touid', $_GET, 'S');
        $this->display('box/mail_add',array(
            'basepath' => '/box/mail',
            'touid' => $touid,
            'agent' => $this->mod_agent_level(),
            'vips' => $this->mod_user_vip(),
            'page_current' => $this->existsKeyInArray('ps',$_GET,'N',1),
            'page_url' => http_build_query($_GET),
        ));
    }
    public function mail_add_pop()
    {
        $touid = $this->existsKeyInArray('touid', $_GET, 'S');
        $agent = $this->mod_agent_level();
        $vips = $this->mod_user_vip();

        $html = '<div id="mainbody">
    <div class="main">
        <div class="info">
            <form name="add_save" action="/box/mail_add_save" method="POST" target="ajax" func="form_submit">
                <dl><dt>标题</dt><dd><input type="text" name="title" class="wtxt" placeholder="短信的主题" /></dd></dl>
                <dl>
                    <dt>收件人</dt>
                    <dd><label><input name="touser" value="touid" type="radio"  checked="checked" />指定UID</label> <label><input name="touser" value="all" type="radio">群发所有</label><br>
                        <input name="users" id="users" value="" class="hide"></dd>
                </dl>
                <dl>
                    <dt>指定收件人ID</dt>
                    <dd><input name="touserid" id="touserid" value="'.$touid.'" class="wtxt" placeholder="玩家的UID，多个玩家请用英文的＂,＂分开"></dd>
                </dl>
                <dl><dt>会员等级</dt><dd><select type="text" name="vip" id="vip">
                            <option value="0" select="selected">请选择</option>';

                            if($vips){
                                foreach($vips as $k=>$v){
                                    $html .= '<option value="'.$k.'">'.$v.'</option>';
                                }
                            }
                            $html .= '
                        </select></dd></dl>
                <dl><dt>代理等级</dt><dd><select type="text" name="agent" id="agent">
                            <option value="0" select="selected">请选择</option>';
                            if($agent){
                                foreach($agent as $k=>$v){
                                    $html .= '<option value="'.$k.'">'.$v.'</option>';
                                }
                            }
                $html .= '</select></dd></dl>
                <dl><dt>短信内容</dt><dd><textarea name="content" class="wtxt" placeholder="短信的内容写在这里，最多660个字" style="width:100%;min-height:120px"></textarea></dd></dl>
                <dl><dt>日期时间</dt><dd><input type="text" name="actiontime" class="int" value="'.date('Y-m-d H:i:s', time()).'" /></dd><dd class="t">格式：'.date('Y-m-d H:i:s', time()).'</dd></dl>
            </form>
        </div>
    </div>
</div>';
        $this->dialogue(array(
            'class' => 'mid',
            'body' => $html,
            'yes'  => array(
                'text' => '发送',
                'func' => '$("form[name=add_save]").submit();',
            ),
            'no'  => array(
                'text' => '关闭',
            ),
        ));
    }
    private function mail_add_edit_args()
    {
        $para = $_POST;
        $args = array(
            'id' => $this->existsKeyInArray('id', $para, 'N'),
            'title' => $this->existsKeyInArray('title', $para, 'S'),
            'touser' => $this->existsKeyInArray('touser', $para, 'S'),
            'touserid' => $this->existsKeyInArray('touserid', $para, 'S'),
            'vip' => $this->existsKeyInArray('vip', $para, 'N'),
            'agent' => $this->existsKeyInArray('agent', $para, 'N'),
            'content' => $this->existsKeyInArray('content', $para, 'S'),
            'actiontime' => $this->existsKeyInArray('actiontime', $para, 'N'),
        );
        if (empty($args['title'])) core::error("短信标题不能为空");
        if (empty($args['content'])) core::error("短信标题不能为空");
        return $args;
    }
    public function mail_add_save()
    {
        if (!$this->post) core::__403();
        $para = $this->mail_add_edit_args();
        unset($para['id']);
        $para['actiontime'] = strtotime($para['actiontime']);
        if($para['touser']=='all')
        {
            $content = $para['content'];
            unset($para['id']);
            unset($para['touid']);
            unset($para['touser']);
            unset($para['touserid']);
            unset($para['content']);
            $insertid = $this->table_row_save('@@mailbox', $para);
            $this->table_row_save('@@mailbox_content', array(
                'forid' => $insertid,
                'content' => $content,
            ));
            $uids = $this->db->query("SELECT `uid` FROM `@@users`",3);
            foreach ($uids as $key=>$uid) {
                $this->table_row_save('@@mailbox_user', array(
                    'forid' => $insertid,
                    'uid' => $uid['uid'],
                ));
            }
            $this->pushClient('all',array(
                'cmd' => 'updatemailtotal',
                'state' => 200,
            ));
        }else{
            $uids = $para['touserid'];
            $content = $para['content'];
            unset($para['id']);
            unset($para['touid']);
            unset($para['touser']);
            unset($para['touserid']);
            unset($para['content']);
            $para['touid'] = $uids;
            $insertid = $this->table_row_save('@@mailbox', $para);
            $this->table_row_save('@@mailbox_content', array(
                'forid' => $insertid,
                'content' => $content,
            ));
            $uids = explode(',', $uids);
            foreach ($uids as $uid) {
                $this->table_row_save('@@mailbox_user', array(
                    'forid' => $insertid,
                    'uid' => $uid,
                ));
                $this->pushClient($uid,array(
                    'cmd' => 'updatemailtotal',
                    'state' => 200,
                ));
            }
        }

        if (!$insertid) core::error("[ {$para['title']} ]添加失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "[" . $para['title'] . "]发送成功",
            'auto' => true,
            'yes' => array(
                'text' => '我知道了',
            ),
        ));
    }
    # region 发件箱
    //获取分布及查找参数
    private function mail_sendlist_get_args($get = true){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $fromTime = date('Y-m-d',time());
        $args['sKey'] = $this->existsKeyInArray('sKey',$para,'S');
        $args['ps'] = $this->existsKeyInArray('ps',$para,'N',$this->pagesize);
        $args['fromTime'] = $this->existsKeyInArray('fromTime',$para,'S',$fromTime);
        $args['toTime'] = $this->existsKeyInArray('toTime',$para,'S',$fromTime);
        return $args;
    }
    //拼装页面参数
    private function mail_sendlist_page_args($args)
    {
        $page_args = array();
        if(!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if(!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        if(!empty($args['fromTime'])) $page_args['fromTime'] = $args['fromTime'];
        if(!empty($args['toTime'])) $page_args['toTime'] = $args['toTime'];
        $page_args['page'] = '{page}';
        return $page_args;
    }
    public function mail_sendlist()
    {
        $basepath = '/box/mail_sendlist';

        $get = true;
        $args = $this->mail_sendlist_get_args($get);
        $page_args = $this->mail_sendlist_page_args($args);
        $args['page_current'] = $this->get_page();

        $datas = $this->mod_mail_sendlist($args);
        $args['page_max'] = $this->get_maxpage($args['ps'],$datas['record']);

        $this->display("box/mail_sendlist",array(
            'basepath' => $basepath,
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'thishref' => '/box/mail_sendlist',
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));

    }

    public function mail_sendlist_delete()
    {
        $id = $this->existsKeyInArray('id',$_GET,'N');
        $title = $this->existsKeyInArray('title',$_GET,'S');
        $this->db->delete('@@mailbox',array('id'=>$id));
        $this->db->delete('@@mailbox_user',array('forid'=>$id));
        $this->db->delete('@@mailbox_content',array('forid'=>$id));
        $this->addLog($this->authid,__FUNCTION__,$id);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "删除[" . $title . "]成功",
            'auto' => true,
            'yes' => array(
                'text' => '我知道了',
                'func' => '$("#btn_refresh").trigger("click");',
            ),
        ));
    }
    # endregion

    # region 推送内容
    public function mail_push_pop()
    {
        $uno = $this->existsKeyInArray('uno', $_GET, 'S');

        $html = '<div id="mainbody">
                    <div class="main">
                        <div class="info">
                            <form name="add_save" action="/box/mail_push_send" method="POST" target="ajax" func="form_submit">
                            <dl><dt>玩家号码</dt><dd><input type="text" name="unos" class="txt" value="'.$uno.'" placeholder="玩家号码，多个玩家请用“,”分开" </dd></dl>
                            <dl><dt>推送类型</dt><dd><select name="t">
                                <option value="ta">以对话框的形式弹出推送内容</option>
                                <option value="to">推送内容自动消失的方式</option>
                                <option value="adt">在新窗口弹出推送内容</option>
                                <option value="adp">在新窗口弹出推送图片</option>
                            </select></dd></dl>
                            <dl><dt>推送图片</dt><dd><input type="text" name="img" class="txt" id="pushicon" placeholder="http://或https://开头" /> </dd></dl>
                            <dl><dt>推送内容</dt><dd><textarea name="body" class="txt" style="width:80%;height:80px;" placeholder="推送的具体内容"></textarea> </dd></dl>
                            </form>
                        </div>
                    </div>
                 </div>';
        $this->dialogue(array(
            'class' => 'big',
            'body' => $html,
            'yes'  => array(
                'text' => '推送',
                'func' => '$("form[name=add_save]").submit();',
            ),
            'no'  => array(
                'text' => '关闭',
            ),
        ));
    }
    # endregion

    # region 组织推送内容
    public function mail_push_send()
    {
        $unos = $this->existsKeyInArray('unos',$_POST,'S');
        $type = $this->existsKeyInArray('t',$_POST,'S');
        $img = $this->existsKeyInArray('img',$_POST,'S');
        $body = $this->existsKeyInArray('body',$_POST,'S');
        $pushcontent = array(
            't'=>$type,
            'msg' => $body,
            'img' => $img,
            'body' => $body,
            'state' => 200,
            'cmd' => 'push',
        );
        switch ($type){
            case 'ta': //纯文字
                unset($pushcontent['img']);
                unset($pushcontent['body']);
                break;
            case 'to': //纯文字自动消失
                unset($pushcontent['img']);
                unset($pushcontent['body']);
                break;
            case 'adt': //文字内容
                unset($pushcontent['img']);
                unset($pushcontent['msg']);
                break;
            case 'adp': //弹出图片
                unset($pushcontent['msg']);
                unset($pushcontent['body']);
                break;
        }
        if($unos=='all'){
            $this->pushClient('all', $pushcontent);
        }else{
            $this->pushClient($unos, $pushcontent);
        }
    }
    # endregion
}